package com.primus.server.security.config;


import com.primus.server.security.authentication.SelfUserDetailsService;
import com.primus.server.security.filter.JwtAuthenticationTokenFilter;
import com.primus.server.security.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    private final SelfUserDetailsService userDetailsService;

    private final SelfAccessDeniedHandler selfAccessDeniedHandler;

    private final SelfAuthenticationEntryPoint selfAuthenticationEntryPoint;

    private final SelfAuthenticationFailureHandler selfAuthenticationFailureHandler;

    private final SelfAuthenticationSuccessHandler selfAuthenticationSuccessHandler;

    private final SelfLogoutSuccessHandler selfLogoutSuccessHandler;

    @Autowired
    public SecurityConfig(JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter,
                          SelfUserDetailsService userDetailsService,
                          SelfAccessDeniedHandler selfAccessDeniedHandler,
                          SelfAuthenticationEntryPoint selfAuthenticationEntryPoint,
                          SelfAuthenticationFailureHandler selfAuthenticationFailureHandler,
                          SelfAuthenticationSuccessHandler selfAuthenticationSuccessHandler,
                          SelfLogoutSuccessHandler selfLogoutSuccessHandler) {
        this.jwtAuthenticationTokenFilter = jwtAuthenticationTokenFilter;
        this.userDetailsService = userDetailsService;
        this.selfAccessDeniedHandler = selfAccessDeniedHandler;
        this.selfAuthenticationEntryPoint = selfAuthenticationEntryPoint;
        this.selfAuthenticationFailureHandler = selfAuthenticationFailureHandler;
        this.selfAuthenticationSuccessHandler = selfAuthenticationSuccessHandler;
        this.selfLogoutSuccessHandler = selfLogoutSuccessHandler;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)

                .and()
                .httpBasic().authenticationEntryPoint(selfAuthenticationEntryPoint)


                .and()
                .authorizeRequests()
                .antMatchers("/css/**","/js/**", "/assets/**","/fonts/**", "/img/**")
                .permitAll()

                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**").anonymous()
                .anyRequest().authenticated()

                .and()
                .formLogin().loginPage("/user/login").permitAll()
                .successHandler(selfAuthenticationSuccessHandler)
                .failureHandler(selfAuthenticationFailureHandler)

                .and()
                .logout().logoutUrl("user/logout")
                .logoutSuccessHandler(selfLogoutSuccessHandler);

        http.headers().cacheControl();

        http.exceptionHandling().accessDeniedHandler(selfAccessDeniedHandler);

        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
